/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.general.bc;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.cip.esymons.general.be.CargoEmpleado;

/**
 *
 * @author CASTOR61
 */
@Stateless
public class CargoEmpleadoFacade implements CargoEmpleadoFacadeLocal {
    @PersistenceContext(unitName = "org.cip.esymons-ejbPU")
    private EntityManager em;

    public void create(CargoEmpleado cargoEmpleado) {
        em.persist(cargoEmpleado);
    }

    public void edit(CargoEmpleado cargoEmpleado) {
        em.merge(cargoEmpleado);
    }

    public void remove(CargoEmpleado cargoEmpleado) {
        em.remove(em.merge(cargoEmpleado));
    }

    public CargoEmpleado find(Object id) {
        return em.find(CargoEmpleado.class, id);
    }

    public List<CargoEmpleado> findAll() {
        return em.createQuery("select object(o) from CargoEmpleado as o").getResultList();
    }

    public List<CargoEmpleado> findRange(int[] range) {
        Query q = em.createQuery("select object(o) from CargoEmpleado as o");
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        return ((Long) em.createQuery("select count(o) from CargoEmpleado as o").getSingleResult()).intValue();
    }

    public List<CargoEmpleado> cargosXEmpleado(Long codigo) {
        try{

        return em.createQuery("select object(o) from CargoEmpleado as o where o.empleado.fkPerCodigopna = ?1")
                .setParameter(1, codigo).getResultList();

        }catch(Exception ex){
            return new ArrayList<CargoEmpleado>();
        }
    }

    public List<CargoEmpleado> empleadosPorCargo(Integer codigo) {
        try{

        return em.createQuery("select object(o) from CargoEmpleado as o where o.cargo.carCodigo = ?1")
                .setParameter(1, codigo).getResultList();

        }catch(Exception ex){
            return new ArrayList<CargoEmpleado>();
        }
    }

}
